package top.glkj.hms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.glkj.hms.dao.StaticValueDao;
import top.glkj.hms.entity.StaticValue;
import top.glkj.hms.service.CommonService;
import top.glkj.hms.service.UserService;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;
import top.glkj.hms.utils.Status;

import java.util.Date;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/12/23 2:32
 */
@Service
@Slf4j
public class CommonServiceImpl implements CommonService {
    private final StaticValueDao staticValueDao;
    private final UserService userService;
    @Autowired
    public CommonServiceImpl(StaticValueDao staticValueDao,UserService userService) {
        this.staticValueDao = staticValueDao;
        this.userService = userService;
    }

    /**
     * 获取静态参数，根据名称或者简码
     *
     * @param name name or code
     * @return Static.values
     */
    @Override
    public Result getStaticValue(String name) {
        StaticValue staticValue = null;
        staticValue = staticValueDao.findStaticValueByName(name);
        if (staticValue == (top.glkj.hms.utils.StaticValue.NULL)) {
            staticValue = staticValueDao.findStaticValueByCode(name);
        }
        return
                (staticValue == (top.glkj.hms.utils.StaticValue.NULL) ?
                        ResultFactory.isError(top.glkj.hms.utils.StaticValue.GET_DATA_ERROR) :
                        ResultFactory.isSuccess(top.glkj.hms.utils.StaticValue.GET_DATA_SUCCESS, staticValue));

    }

    /**
     * 添加静态参数
     *
     * @param staticValue 静态参数
     * @return true or false
     */
    @Override
    public Result addStaticValue(StaticValue staticValue) {
        staticValue.setCreateTime(new Date());
        return saveEntity(staticValue);
    }

    /**
     * 修改静态参数
     *
     * @param staticValue 静态参数
     * @return true or false
     */
    @Override
    public Result updateStaticValue(StaticValue staticValue) {
        return saveEntity(staticValue);
    }



    private Result saveEntity(StaticValue staticValue) {
        try {
            staticValue.setUpdateTime(new Date());
            StaticValue save = staticValueDao.save(staticValue);
            return ResultFactory.isSuccess(top.glkj.hms.utils.StaticValue.SUCCESS_MESSAGE, save);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultFactory.isError(top.glkj.hms.utils.StaticValue.ERROR_MESSAGE, null);
        }
    }

    /**
     * 获取所有状态
     *
     * @return status
     */
    @Override
    public Result getAllStatus() {
        return ResultFactory.isSuccess(Status.values());
    }

    /**
     * 根据状态id返回状态集合
     *
     * @param id 状态组id
     * @return 状态列表集合
     */
    @Override
    public Result getStatusById(int id) {
        return null;
    }
}
